<?php
/**
 * 住宅区
 * 
 * Created	: 2010-12-01
 * Modified : 2010-12-01
 * @author  : 陈立强
 */
class Uptown{
	
	public static $cfg = null;
	public static $tbl = 'uptown';

	/**
	 * 初始化生成住宅数据
	 * @param int $street_id
	 * @return boolean
	 */
	public static function init($streetId)
	{
		global $DB, $L;
		$area = 1;
		$sql = 'SELECT * FROM street WHERE street_id='.$streetId;
		$streetInfo = $DB->getValue($sql);
		for($i=1; $i<=10; $i++) {
			$data = array(
				'area' => 0,
				'street_id' => $streetId,
				'uid' => 0,
				'popularity' => 0,
			);
		}
		return true;
	}
	
	public static function addViePoint($uid, $point)
	{
		global $DB, $L, $G;
		$shopInfo = $DB->getValue('SELECT * FROM user_shop WHERE uid='.$uid);
	}
	
	/**
	 * 住宅区竞争
	 * @param int $uid
	 */
	public static function vie($uid, $ut_id)
	{
		global $DB, $L, $G;

		$ut_id = (int)$ut_id;
		$now = time();
		$shopInfo = $DB->getValue('SELECT * FROM user_shop WHERE uid='.$uid);
		$utInfo = $DB->getValue('SELECT * FROM uptown WHERE ut_id='.$ut_id);
		
		$shop_id = $shopInfo['shop_id'];
		
		$toInfo = $DB->getValue('SELECT * FROM user_shop WHERE shop_id='.$utInfo['shop_id']);
		
		//-- 竞争者的区域和住宅区不同
		if ($shopInfo['area'] != $utInfo['area']) {
			return array(
				'val' => false,
				'msg' => $L['shop_uptown_area_mismatch'],
			);
		}
		
		$cfg = Street::$cfg[$utInfo['area']];
		$addPop = $cfg['add_pop'];
		
		$DB->beginTransaction();
		
		//-- 计算竞争结果
		
		$fightResult = Shop::fight($shopInfo, $toInfo);
		$winner = $fightResult[0];
		$vieProcess = $fightResult[1];
		
		$data = array(
			'shop_id' => $winner,
		);
		$where = 'ut_id='.$ut_id;
		$ret = $DB->update('uptown', $data, $where);
		
		if (false !== $ret) {
			$data = array(
				'uptown_add_pop' => $shopInfo['uptown_add_pop'] + $addPop
			);
			$where = 'shop_id='.$shop_id;
			$ret = $DB->update('user_shop', $data, $where);
			if (false !== $ret) {
				// 如果住宅区原来有玩家占领，将原来玩家的人气加成扣除
				if ($utInfo['shop_id']) {
					$data = array(
						'uptown_add_pop' => $toInfo['uptown_add_pop'] - $addPop
					);
					$where = 'ut_id='.$ut_id;
					$ret = $DB->update('user_shop', $data, $where);
					if (false !== $ret) {
						$DB->commit();
						$result['data'] = array(
							'add'    => $addPop,
							'winner' => $winner
						);
					} else {
						$DB->rollBack();
						$result['msg'] = $L['Failure_Update'];
						$str = var_export($DB->errors, true);
						// 记录错误日志
						Log::save('uptown_vie_update_uptown', $str);
					}
				} else {
					$DB->commit();
					$result['data'] = array(
						'add'    => $addPop,
						'winner' => $winner
					);
				}
			} else {
				$DB->rollBack();
				$result['msg'] = $L['Failure_Update'];
				$str = var_export($DB->errors, true);
				// 记录错误日志
				Log::save('uptown_vie_update_user_shop', $str);
			}
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('uptown_vie_update_uptown', $str);
		}
		return $result;
	}
}